package md.lanjing.com.module_home.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.R2;
import md.lanjing.com.module_home.bean.ForgetLoginPwdResponse;
import md.lanjing.com.module_home.bean.ForgetPwdResponse;
import md.lanjing.com.module_home.contact.ForgetLoginPwdContact;
import retrofit2.Response;

public class ForgetLoginPwdActivity extends MvpActivity<ForgetLoginPwdContact.ForgetLoginPwdPresent> implements ForgetLoginPwdContact.IForgetLoginPwdView {
    @BindView(R2.id.et_tel_number)
    EditText etTelNumber;
    @BindView(R2.id.et_security_code)
    EditText etSecurityCode;
    @BindView(R2.id.btn_get_security_code)
    TextView btnGetSecurityCode;
    @BindView(R2.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R2.id.et_confirm_pwd)
    EditText etConfirmPwd;


    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_login_pwd;
    }


    @Override
    public void getForgetLoginPwdData(Response<ForgetLoginPwdResponse> response) {
        dismissLoadingDialog();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"找回密码成功");
            finish();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    public void getSecurityCode(Response<ForgetPwdResponse> response) {
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            CountDownTimerUtils countDownTimerUtils=new CountDownTimerUtils(btnGetSecurityCode,60000,1000);
            countDownTimerUtils.start();
            ToastUtils.showLongToast(context,"验证码发送成功");
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }


    @Override
    protected ForgetLoginPwdContact.ForgetLoginPwdPresent createPresent() {
        return new ForgetLoginPwdContact.ForgetLoginPwdPresent();
    }


    @OnClick({R2.id.btn_get_security_code, R2.id.btn_confirm_modify})
    public void onViewClicked(View view) {
        String tel = etTelNumber.getText().toString();
        int i = view.getId();
        if (i == R.id.btn_get_security_code) {
            if (ObjectUtils.isEmpty(tel)) {
                ToastUtils.showLongToast(context, "手机号不能为空");
                return;
            }
            if (!RegexUtils.isMobileExact(tel)) {
                ToastUtils.showLongToast(context, "请输入正确的手机号");
                return;
            }
            mPresent.getForgetPwdSecurityCode(context, tel);

        } else if (i == R.id.btn_confirm_modify) {
            String code = etSecurityCode.getText().toString();
            String pwd = etNewPwd.getText().toString();
            String confirmPwd = etConfirmPwd.getText().toString();

            if (ObjectUtils.isEmpty(tel)) {
                ToastUtils.showLongToast(context, "手机号不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(code)) {
                ToastUtils.showLongToast(context, "验证码不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(pwd)) {
                ToastUtils.showLongToast(context, "新密码不能为空");
                return;
            }
            if (ObjectUtils.isEmpty(confirmPwd)) {
                ToastUtils.showLongToast(context, "确认密码不能为空");
                return;
            }
            if (!RegexUtils.isMobileExact(tel)) {
                ToastUtils.showLongToast(context, "请输入正确的手机号");
                return;
            }
            if (!ObjectUtils.equals(pwd, confirmPwd)) {
                ToastUtils.showLongToast(context, "两次密码输入不一致");
                return;
            }
            if (!RegexUtils.isLoginPwd(pwd)) {
                ToastUtils.showLongToast(context, getString(R.string.login_pwd_regex));
                return;
            }
            showLoadingDialog();

            mPresent.forgetLoginPwd(context, code, pwd, tel);

        }
    }
}
